home *** CD-ROM | disk | FTP | other *** search
/ Super Shareware Collection / Super Shareware Collection.iso / os_2 / memsz220.zip / DEBUG.CPP < prev    next >
Text File  |  1994-02-07  |  7KB  |  227 lines

  1. /******************************************************************* DEBUG.CC
  2.  *                                        *
  3.  *  Debugging Aids                                *
  4.  *                                        *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_BASE
  8. #define INCL_WIN
  9. #include <os2.h>
  10.  
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #include "debug.h"
  17.  
  18. extern HFILE Timer = 0 ;
  19. extern BOOL Trace = FALSE ;
  20.  
  21.  
  22. /****************************************************************************
  23.  *                                        *
  24.  *             Display Debug Message                    *
  25.  *                                        *
  26.  ****************************************************************************/
  27.  
  28. extern VOID Debug ( HWND hwnd, char *Message, ... )
  29. {
  30.  /***************************************************************************
  31.   * Local Declarations                                *
  32.   ***************************************************************************/
  33.  
  34.   va_list Marker ;
  35.   char Text [500] ;
  36.  
  37.  /***************************************************************************
  38.   * Format the debug message.                            *
  39.   ***************************************************************************/
  40.  
  41.   va_start ( Marker, Message ) ;
  42.   vsprintf ( Text, Message, Marker ) ;
  43.   va_end ( Marker ) ;
  44.  
  45.  /***************************************************************************
  46.   * Display the log message and wait for the user to press ENTER.        *
  47.   ***************************************************************************/
  48.  
  49.   WinMessageBox ( HWND_DESKTOP, hwnd, (PSZ)Text, (PSZ)"Debug", 0, MB_ENTER ) ;
  50. }
  51.  
  52. /****************************************************************************
  53.  *                                        *
  54.  *               Log Debug Message                    *
  55.  *                                        *
  56.  ****************************************************************************/
  57.  
  58. extern VOID Log ( char *Message, ... )
  59. {
  60.  /***************************************************************************
  61.   * Try to open the log file.  If unsuccessful, just return.            *
  62.   ***************************************************************************/
  63.  
  64.   ULONG Action ;
  65.   HFILE Handle ;
  66.  
  67.   if ( DosOpen ( (PSZ)"LOG", &Handle, &Action, 0,
  68.     FILE_NORMAL, FILE_CREATE | FILE_OPEN,
  69.     OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE | OPEN_FLAGS_FAIL_ON_ERROR | 
  70.     OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_SEQUENTIAL, 0 ) )
  71.   {
  72.     return ;
  73.   }
  74.  
  75.  /***************************************************************************
  76.   * Position to the end of the file.                                        *
  77.   ***************************************************************************/
  78.  
  79.   ULONG Position ;
  80.   DosChgFilePtr ( Handle, 0, FILE_END, &Position ) ;
  81.  
  82.  /***************************************************************************
  83.   * Format the message for the log file.                                    *
  84.   ***************************************************************************/
  85.  
  86.   char Buffer [512] ;
  87.   va_list Marker ;
  88.  
  89.   va_start ( Marker, Message ) ;
  90.   vsprintf ( Buffer, Message, Marker ) ;
  91.   va_end ( Marker ) ;
  92.  
  93.  /***************************************************************************
  94.   * Write the message to the log file.                                      *
  95.   ***************************************************************************/
  96.  
  97.   ULONG Written ;
  98.  
  99.   DosWrite ( Handle, Buffer, strlen(Buffer), &Written ) ;
  100.  
  101.  /***************************************************************************
  102.   * Close the log file and return.                        *
  103.   ***************************************************************************/
  104.  
  105.   DosClose ( Handle ) ;
  106. }
  107.  
  108. /****************************************************************************
  109.  *                                        *
  110.  *                Open Timer for Use                    *
  111.  *                                        *
  112.  ****************************************************************************/
  113.  
  114. extern BOOL OpenTimer ( VOID )
  115. {
  116.   ULONG Action ;
  117.  
  118.   if ( Timer )
  119.     DosClose ( Timer ) ;
  120.  
  121.   if ( DosOpen ( (PSZ)"TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  122.   {
  123.     return ( FALSE ) ;
  124.   }
  125.  
  126.   return ( TRUE ) ;
  127. }
  128.  
  129. /****************************************************************************
  130.  *                                        *
  131.  *                Close Timer                    *
  132.  *                                        *
  133.  ****************************************************************************/
  134.  
  135. extern VOID CloseTimer ( VOID )
  136. {
  137.   DosClose ( Timer ) ;
  138. }
  139.  
  140. /****************************************************************************
  141.  *                                        *
  142.  *             Read Time from HRTIMER.SYS                *
  143.  *                                        *
  144.  ****************************************************************************/
  145.  
  146. extern BOOL GetTime ( PTIMESTAMP pts )
  147. {
  148.   ULONG ByteCount ;
  149.  
  150.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  151.     return ( FALSE ) ;
  152.  
  153.   return ( TRUE ) ;
  154. }
  155.  
  156. /****************************************************************************
  157.  *                                        *
  158.  *               Calculate Elapsed Time                *
  159.  *                                        *
  160.  ****************************************************************************/
  161.  
  162. extern ULONG ComputeElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  163. {
  164.   ULONG ulMsecs, ulNsecs;
  165.   TIMESTAMP tsStart, tsStop ;
  166.  
  167.   tsStart = *ptsStart ;               // De-reference timestamp
  168.                           //     structures for speed
  169.   tsStop  = *ptsStop ;
  170.  
  171.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  172.  
  173.   if( tsStart.ulNs > tsStop.ulNs )          // If nanosecond overflow ...
  174.   {
  175.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  176.     ulMsecs--;                          // Adjust milliseconds
  177.   }
  178.   else
  179.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  180.  
  181.   *pulNs = ulNsecs ;
  182.  
  183.   return ( ulMsecs ) ;
  184. }
  185.  
  186. /****************************************************************************
  187.  *                                        *
  188.  *  Allocate Memory                                *
  189.  *                                        *
  190.  ****************************************************************************/
  191.  
  192. //#define ALLOCATE_THROUGH_DOS
  193.  
  194. extern PVOID AllocateMemory ( ULONG ByteCount )
  195. {
  196.   #ifdef ALLOCATE_THROUGH_DOS
  197.   {
  198.     PVOID Memory ;
  199.     DosAllocMem ( &Memory, ByteCount, PAG_READ | PAG_WRITE | PAG_COMMIT ) ;
  200.     return ( Memory ) ;
  201.   }
  202.   #else
  203.   {
  204.     return ( malloc ( ByteCount ) ) ;
  205.   }
  206.   #endif
  207. }
  208.  
  209. /****************************************************************************
  210.  *                                        *
  211.  *  Free Memory                                 *
  212.  *                                        *
  213.  ****************************************************************************/
  214.  
  215. extern VOID FreeMemory ( PVOID Memory )
  216. {
  217.   #ifdef ALLOCATE_THROUGH_DOS
  218.   {
  219.     DosFreeMem ( Memory ) ;
  220.   }
  221.   #else
  222.   {
  223.     free ( Memory ) ;
  224.   }
  225.   #endif
  226. }
  227.